package com.gin.kotlin.no_gay_alliance.adapter.post

import android.content.Context
import android.view.View
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.databinding.ItemTopicKeyBinding
import com.gin.kotlin.no_gay_alliance.enums.HandlerMessageWhat
import com.gin.nga.response.topic.TopicKey

class TopicKeyAdapter(context: Context, handler: MyHandler) : AbstractBindingRecyclerViewAdapter<ItemTopicKeyBinding, TopicKey>(context, handler, false),
    View.OnClickListener {
    override fun onBindViewHolder(holder: ViewHolder<ItemTopicKeyBinding>, position: Int, b: ItemTopicKeyBinding, dataModel: TopicKey) {
        b.tvTopicKey.text = dataModel.tag
        if (dataModel.locked == true) {
            val drawable = getDrawable(R.drawable.ic_lock_24)
            b.tvTopicKey.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
        } else
            b.tvTopicKey.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)


        listOf(b.root, b.tvTopicKey).forEach {
            it.setOnClickListener(this)
            it.tag = dataModel.tag
        }
    }

    override fun onClick(v: View?) {
        val text = v?.tag as? String ?: return
        handler.sendMessage(HandlerMessageWhat.ON_CLICK_TOPIC_KEY, text)
    }
}